<template>
  <van-nav-bar :title="t('home.销毁')" left-arrow @click-left="onClickLeft" />
  <div class="mx-auto mb-20 ">
    <van-cell-group>
      <van-field>
        <template #input>
          {{curItem['index']}}/{{ dataList.length }}
        </template>
      </van-field>

      <van-field  :label="t('returnDestroy.原始单号')" :model-value="curItem['originalNumber']"> 
      </van-field> 
      <van-field  :label="t('returnDestroy.库位')" :model-value="curItem['storageBlock']"> 
      </van-field> 
      
      <van-field
        v-model="scan"
        :label="t('returnDestroy.单号')"
        :placeholder="t('returnDestroy.请扫描退件单号')"
        @keydown.enter="handleScan"
        clearable
        id="scan_order"
      />

      <van-field>
        <template #input>
          <div :style="info==t('returnDestroy.操作成功')?'color:green':'color:red'">
          {{ info }}</div>
        </template>
      </van-field>
    </van-cell-group>
 
  </div>

  <div class="absolute inset-x-0 bottom-0">
      <van-field>
        <template #input>
          <van-button
            type="success"
            size="small"
            @click="
              () => { 
                curItem = {
                  index: curItem['index'] - 1,
                  ...dataList[curItem['index'] - 2]}
              }
            "
            class="w-full p-8"
            >{{ t("returnDestroy.上一个") }}</van-button
          >
          <van-button
            type="success"
            size="small"
            @click="
              () => {
                curItem = {
                  index: curItem['index'] + 1,
                  ...dataList[curItem['index']]
                } 
              }
            "
            class="w-full p-8"
            >{{ t("returnDestroy.下一个") }}</van-button
          >
        </template>
        
      </van-field> 
    </div>
</template>

<script setup lang="ts">
// import img_index_path from "~/noData.png";
import { postRequest } from "@/api";
import { useI18n } from "vue-i18n";
import { AppToast } from "@/utils/appToast";
// import { DEPOT_CODE } from "@/utils/constant";
const { t } = useI18n();

onMounted(() => {
  const scan_order = document.getElementById("scan_order");
  scan_order.focus();
});

const onClickLeft = () => history.back();
const scan = ref("");
const OPERTYPE = ref("");
const info = ref("");
 
 
const dataList = ref([]);
const curItem = ref({});
// localStorage.setItem('userInfo',JSON.stringify({"account":"王俊","password":"123456","organizationName":"技术产品中心","token":"5c34bd829f7c4495b23d70b29247e3c1"}))
// localStorage.setItem('token','123')
function getPathParamsByName(name) {
  const reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  const r = window.location.search.substr(1).match(reg);
  if (r != null) return unescape(r[2]);
  return null;
}
const init = async () => {
  OPERTYPE.value = getPathParamsByName("operType");
  // dataList.value = JSON.parse(getPathParamsByName("dataList"));

  postRequest("/ReturnOrderService/tms/return/queryWaitDestory", {
        // isRetrying: true,
        // status:2,
        // depot: DEPOT_CODE,
        offset: 0,
        limit: 1000,
      }).then((res) => {
        if (res.status === 200) { 
          dataList.value  = res.data;
          // 按照 storageBlock 排序
          dataList.value.sort((a, b) => {
            return a.storageBlock > b.storageBlock ? 1 : -1;
          });
          curItem.value = {
            index: 1,
            ...dataList.value[0]
          }
          
          // tabs.offset[tabType] = 100;
          // tabs.page[tabType] = 1;
          AppToast(t("returnDestroy.加载成功"));
        } else {
          // AppToast(res.message, "fail");
        }
      });

};
init();

const handleScan = () => {
  info.value = "";
  // 判断 curItem.originalNumber 是否等于 scan

  // destory 
  if(curItem.value['originalNumber']!=scan.value){
    AppToast(t("returnDestroy.单号不匹配"), "fail",true);
    info.value = t("returnDestroy.单号不匹配");
    scan.value = "";
    return
  }
  // AppToast(t("fail") + " " + "待开发", "fail", true);
  // destory   packageNumber
  postRequest("/ReturnOrderService/tms/return/destory?packageNumber="+ scan.value, { 
        packageNumber:  scan.value,
      }).then((res) => {
        if (res.status === 200) {  
          AppToast(t("returnDestroy.销毁成功"));
          info.value = t("returnDestroy.销毁成功");
        } else {
          AppToast(t("returnDestroy.销毁失败")+" "+res.message, "fail", true);
          info.value = t("returnDestroy.销毁失败")+" "+res.message;
        }
        scan.value = "";
      });
  return 
};
</script>

<style lang="less" scoped></style>
